import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import { getuserinfoAPI } from '@/api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '', // 保存koken字符串
    userinfo: {} // 保存用户信息(对象)
  },
  getters: {
    // username (state) {
    //   return state.userinfo.username
    // }
    // username: (state) => {
    //   return state.userinfo.username
    // }
    username: state => state.userinfo.username, // 用户名
    nickname: state => state.userinfo.nickname, // 用户昵称
    user_pic: state => state.userinfo.user_pic // 用户头像
  },
  mutations: {
    // 保存token到vuex中
    updatetoken (state, val) {
      state.token = val
    },
    // 保存用户信息
    updateuserinfo (state, val) {
      state.userinfo = val
    }
  },
  actions: {
    // 请求用户的信息
    async getuserinfoactions (store) {
      const res = await getuserinfoAPI()
      // console.log(res)
      store.commit('updateuserinfo', res.data.data)
    }
  },
  modules: {
  },
  plugins: [
    createPersistedState()
  ]
})
